package com.example.crm.servlet;
import com.example.crm.dao.CustomerDAO;
import com.example.crm.domain.Customer;
import com.example.crm.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addcustomer")
public class InsertCheckServlet extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


		req.setCharacterEncoding("utf-8");

		HttpSession s = req.getSession();
		User user = (User) s.getAttribute("user");

		Customer c = new Customer();

		c.setName(req.getParameter("cust_name"));

		c.setUserId(user.getId());
		c.setCreateId(user.getId());
		c.setSource(req.getParameter("cust_source"));
		c.setIndustry(req.getParameter("cust_industry"));
		c.setLevel(req.getParameter("cust_level"));
		c.setLinkman(req.getParameter("cust_linkman"));
		c.setPhone(req.getParameter("cust_phone"));
		c.setMobile(req.getParameter("cust_mobile"));


		// 2. 调用DAO
		CustomerDAO dao = new CustomerDAO();
		try {
			int result = dao.insert(c);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 3. 跳转列表页
		//resp.sendRedirect("jsp/customer/list.jsp");
		resp.sendRedirect("listcustomer");

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
